<?php

class loginpolice extends Controller {

	function loginpolice()
	{
		parent::Controller();
		
		$this->template->add_css('styles/style.css');
		$this->template->write('title', 'Bangalore City Police');
		  $this->template->add_js('js/jquery-1.3.2.js');	
		
	}
	
	function index()
	{		
		
		
		$this->loginapp();
	}
	function testing(){
		echo "from Testing";
		//$this->template->set('title', 'About mesdf');
		//$this->template->write_view('header','header',$headerdata, True);
		//$this->template->load('template', 'about');
	}
	//Login Methdods
	function loginapp($error=''){
		$data=array('erroralert'=>$error);
		
		$this->template->write_view('header','header',$data,True);
		$this->template->write_view('content', 'others/loginview');
		$this->template->render();
	}
	function validateuser(){
		$username=$_POST['txtusernme'];
		$password=md5($_POST['txtpassword']);
		$this->load->database();
		
		//Check User
		$qryUserCheck = $this->db->query("select *,count(user_id) as ct from tbl_userdetails where user_name='".$username."' and user_password='".$password."'");
		$qryUserrow=$qryUserCheck->result();
		$qryUserrow=$qryUserrow[0];
		
		$error='';
		if ($qryUserrow->ct==1){
			if ($qryUserrow->published==0){
				$error="Your User Name: ".$username." is Blocked ";	
			}else{
			//Set Session Move to Dashboard
			$this->load->library('session');
			$userArray=array();
			$userArray['user_id']=$qryUserrow->user_id;
			$userArray['user_name']=$qryUserrow->user_name;
			$userArray['ps_id']=$qryUserrow->ps_id;
			$userArray['grp_id']=$qryUserrow->grp_id;			
			$userArray['loggedat']=time();
			if ($qryUserrow->user_password=='21232f297a57a5a743894a0e4a801fc3'){
				
				$userArray['pschange']=1;
			}else{
				$userArray['pschange']=0;
			}
			
			//Other Info
			$tempObject=$this->getLoggedInfo($qryUserrow->ps_id);
			$userArray['ps_name']=$tempObject->ps_name;
			$userArray['div_name']=$tempObject->div_name;
			
			$this->session->set_userdata('userlogindetails',$userArray);		
			}
		}else{
			$error="Invalid Username/Password";
		}
		
		//Redirect the App As per conf
		if (empty($error)){
			redirect('/loginpolice/userdashboard');
		}else{
			$this->loginapp($error);
		}
		 
	}
	function getLoggedInfo($ps){
		$this->load->database();
		
		//Check User
		$qryStationCheck = $this->db->query("SELECT * from tbl_station a join tbl_division b on a.div_id=b.div_id where a.ps_id=".$ps);
		$qryStationrow=$qryStationCheck->result();
		$qryStationrow=$qryStationrow[0];
		return $qryStationrow;
	}
	function userdashboard(){		
		checkAppSession();
		
		$data=array('userarray'=>$this->session->userdata('userlogindetails'));
		//$userArray=$this->session->userdata('userlogindetails');	
		//echo $userArray['ps_name'];
		
		$this->template->write_view('header','header',$data,True);
		$this->template->write_view('content', 'others/dashboard');
		$this->template->render();
	}
	function userhelp(){		
		checkAppSession();
		
		$data=array('userarray'=>$this->session->userdata('userlogindetails'));
		//$userArray=$this->session->userdata('userlogindetails');	
		//echo $userArray['ps_name'];
		
		$this->template->write_view('header','header',$data,True);
		$this->template->write_view('content', 'others/helpfile');
		$this->template->render();
	}
	function logoutapplication(){
		$this->load->library('session');
		
		$this->session->unset_userdata('userlogindetails');
		
		redirect('loginpolice');
	}
	function changepassword(){
		$this->load->library('session');
		checkAppSession();
		$this->load->database();
		$userArray=$this->session->userdata('userlogindetails');		
		
		$displayform='';
		$displayform.='<form action="/loginpolice/savepassword" method="post" name="police_firform" id="police_firform">';
		$displayform.='<table align="center" cellpadding="5" width="80%">
					<tr>
						<td>User Name: </td>
						<td>'.$userArray['user_name'].'</td>
					</tr>
					<tr>
						<td>Enter New Password: </td><td align="left"><input type="password" name="password1" id="password1" /></td>
					</tr>
					<tr>
						<td>Confirm Password: </td><td align="left"><input type="password" name="password2" id="password2" /></td>
					</tr>
					<tr>
						<td></td><td><input type="submit" name="submit"  value="Submit" />
						
						<input id="btnBackarray" class="" type="button" onclick="javascript: history.go(-1)" value="Cancel" name="btnBackarray"/>
						</td>
					</tr>
					</table>
			';
		$displayform.='</form>';	
		
		$data = array('formdisplay'=>$displayform, 'pageHeader'=>'Manage Password','userarray'=>$this->session->userdata('userlogindetails'));
		$this->template->add_js('js/jquery.validate.js');
		$this->template->add_js('
			$(function() {
			
				$("#police_firform").validate({
  				rules: {
    				password1: {
					
					minlength: 6
				},
				password2: {
					
					minlength: 6,
					equalTo: "#password1"
				}

 				 }
			});
			
			});
		
		', 'embed');
		
		
		$this->template->write_view('header','header',$data,True);
        $this->template->write_view('content', 'stations/registrationfir', $data, True);
		$this->template->render();
	}
	function savepassword(){
		$this->load->library('session');
		$this->load->database();
		$userArray=$this->session->userdata('userlogindetails');
		$upData=array(
			'user_password'=>md5($_POST['password1'])
		);	
		$userArray['pschange']=0;	
		$this->session->set_userdata('userlogindetails',$userArray);
		$arrWhere=array('user_id'=>$userArray['user_id']);
			$this->db->update('tbl_userdetails', $upData,$arrWhere); 
			$this->maskredirect('/loginpolice/userdashboard');
	}
	function maskredirect($par){
		redirect($this->config->item('base_url').$par);
	}
/*	function update_userid(){
		$this->load->database();
		$query = $this->db->query("select * from tbl_station");
		foreach ($query->result() as $row)
		{
   			echo $row->ps_name;
   			
			if (!empty($row->ps_shortcode)){
			echo $row->ps_name;
   			$arr=array(
				'user_name'=>$row->ps_shortcode,
				'user_password'=>md5('admin'),
				'grp_id'=>2,
				'ps_id'=>$row->ps_id,
				'published'=>1
			);
			$this->db->insert('tbl_userdetails', $arr); 
			}
		}
	}*/
	
}
